package com.seamtop.cuber.web.controller.cuber;

import com.seamtop.cuber.common.base.DataObject;
import com.seamtop.cuber.common.entriy.CuberCounter;
import com.seamtop.cuber.common.entriy.CuberCounterSet;
import com.seamtop.cuber.common.entriy.CuberReducer;
import com.seamtop.cuber.common.metadata.TableMetaData;
import com.seamtop.cuber.web.service.CounterService;
import com.seamtop.cuber.web.service.ReducerService;
import com.seamtop.cuber.web.service.TableService;
import org.dom4j.Document;
import org.dom4j.io.XMLWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;

/**
 * Created by zongjunfeng on 2015/10/16.
 */

@Controller("ConfInfoController")
public class ConfInfoController {

    @Autowired
    private TableService tableService;

    @Autowired
    private CounterService counterService;

    @Autowired
    private ReducerService reducerService;

    @RequestMapping("/conf/tableSchema.shtml")
    public @ResponseBody
    void tableSchema(HttpServletRequest request, HttpServletResponse response,
                         ModelMap model) {
        HashMap<String,TableMetaData> tableMetaDataHashMap = DataObject.getMetaDataMap();
        Document t_document = tableService.transTruthTableMDataXml(tableMetaDataHashMap);
        try {
            response.setContentType("text/xml;charset=utf-8");
            response.setHeader("cache-control", "no-cache");
            XMLWriter w = new XMLWriter(response.getWriter());
            w.write(t_document);
            w.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    @RequestMapping("/conf/tableVirtuals.shtml")
    public @ResponseBody
    void tableVirtuals(HttpServletRequest request, HttpServletResponse response,
                     ModelMap model) {
        HashMap<String,TableMetaData> tableMetaDataHashMap = DataObject.getMetaDataMap();
        Document vt_document = tableService.transVirtualTableMDataXml(tableMetaDataHashMap);
        try {
            response.setContentType("text/xml;charset=utf-8");
            response.setHeader("cache-control", "no-cache");
            XMLWriter w = new XMLWriter(response.getWriter());
            w.write(vt_document);
            w.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    @RequestMapping("/conf/tableCounter.shtml")
    public @ResponseBody
    void tableCounter(HttpServletRequest request, HttpServletResponse response,
                      ModelMap model) {
        HashMap<String,CuberCounter> cuberCounterMap = DataObject.getCuberCounterMap();
        HashMap<String,CuberCounterSet> counterSetMap = DataObject.getCuberCounterSetMap();
        Document counterDocument = counterService.transCuberCounterXml(cuberCounterMap, counterSetMap);
        try {
            response.setContentType("text/xml;charset=utf-8");
            response.setHeader("cache-control", "no-cache");
            XMLWriter w = new XMLWriter(response.getWriter());
            w.write(counterDocument);
            w.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @RequestMapping("/conf/tableReducer.shtml")
    public @ResponseBody
    void tableReducer(HttpServletRequest request, HttpServletResponse response,
                      ModelMap model) {
        HashMap<String,CuberReducer> cuberReducerMap = DataObject.getCuberReducerMap();
        Document reducerDocument = reducerService.transCuberReducerXml(cuberReducerMap);
        try {
            response.setContentType("text/xml;charset=utf-8");
            response.setHeader("cache-control", "no-cache");
            XMLWriter w = new XMLWriter(response.getWriter());
            w.write(reducerDocument);
            w.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }





}
